package control
{
	import event.GotoReaderEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import model.GamebookReaderModel;
	import model.entity.Chapter;
	import model.entity.Gamebook;
	import model.entity.Option;
	
	import mx.collections.ArrayCollection;
	
	import service.GamebookReaderService;

	public class GamebookReaderController
	{	
		protected var _gamebookreaderModel:GamebookReaderModel;
		public function set gamebookReaderModel(value:GamebookReaderModel):void
		{
			_gamebookreaderModel = value;
		}
		
		protected var _gamebookreaderService:GamebookReaderService;
		public function set gamebookReaderService(value:GamebookReaderService):void
		{
			_gamebookreaderService = value;
		}
		
		[Bindable] 
		public var _xmlcatalog:XML;
		[Bindable]
		public function get xmlcatalog():XML
		{
			return _xmlcatalog;
		}
		public function set xmlcatalog(value:XML):void
		{
			if (_xmlcatalog != value)
			{
				_xmlcatalog = value;
				parseXmlCatalog();
			}
		}
		
		[Bindable]
		private var _xmlbook:XML;
		[Bindable]
		public function get xmlbook():XML
		{
			return _xmlbook;
		}
		
		public function set xmlbook(value:XML):void
		{
			if (_xmlbook != value)
			{
				_xmlbook = value;
				parseXmlBook();
			}
		}
		
		public function parseXmlCatalog():void
		{
			_gamebookreaderModel.books = new ArrayCollection();
			
			for each (var xmlchild:XML in xmlcatalog.children())
			{
				var book:Gamebook = new Gamebook();
				book.author = xmlchild.child("author").children().toXMLString();
				book.title = xmlchild.child("title").children().toXMLString();
				book.cover = xmlchild.child("cover").children().toXMLString();
				book.genre = xmlchild.child("genre").children().toXMLString();
				book.year = xmlchild.child("year").children().toXMLString();
				book.file = xmlchild.child("file").children().toXMLString();
				for each (var xmluploadedchild:XML in xmlchild.child("uploaded"))
				{
					book.upday = xmluploadedchild.child("day").children().toXMLString();
					book.upmonth = xmluploadedchild.child("month").children().toXMLString();
					book.upyear = xmluploadedchild.child("year").children().toXMLString();
				}
				_gamebookreaderModel.books.addItem(book);
			}
		}
		
		public function parseXmlBook():void
		{
			_gamebookreaderModel.chapters = new ArrayCollection();
			
			for each (var xmlchild:XML in xmlbook.children())
			{
				var chapter:Chapter = new Chapter();
				chapter.options = new ArrayCollection();
				chapter.id = xmlchild.child("ID").children().toXMLString();
				chapter.text = xmlchild.child("TEXT").children().toXMLString();
				
				for each (var xmloptionchild:XML in xmlchild.child("OPTIONS").children())
				{
					var option:Option = new Option();
					option.text = xmloptionchild.children().toXMLString();
					option.goto = xmloptionchild.attribute("goto").toXMLString();
					chapter.options.addItem(option);
				}
				_gamebookreaderModel.chapters.addItem(chapter);
			}
		}
		
		
		public function initxmlcatalog():void
		{
			var XMLLoader:URLLoader = new URLLoader();
			XMLLoader.load(new URLRequest("asset/xml/catalog.xml"));
			XMLLoader.addEventListener(Event.COMPLETE, processCatalogXML);
		}
		
		public function processCatalogXML(e:Event):void 
		{
			xmlcatalog = new XML(e.target.data);
		}
		
		public function handleBookChoice(e:GotoReaderEvent):void
		{
			var XMLLoader:URLLoader = new URLLoader();
			XMLLoader.load(new URLRequest(e.file));
			XMLLoader.addEventListener(Event.COMPLETE, processBookXML);
		}
		
		public function processBookXML(e:Event):void 
		{
			xmlbook = new XML(e.target.data);
		}
		
	}
}